import type { IQuery } from '@/api/config/types';
import { fetchNoticeList } from '@/api/modules/system/notice/notice';
import type { DesktopNotice } from './type';

export const showNotice = ref(false);
export const noticeLoading = ref(false);
export const noticeList = ref<DesktopNotice[]>([]);
const query = ref<IQuery>({
  pageNum: 1,
  pageSize: 30,
  total: 0,
  isAsc: 'desc',
  orderByColumn: 'create_time',
});
/* 上线提示 */
export const onlinePopover = ref({
  show: false,
  content: '',
});

/* 当前通知的类型，用于区分不同通知 */
export const currentNoticeType = ref('');

/**
 * @description: 获取系统通知
 */
export const getSystemNotice = async () => {
  noticeLoading.value = true;
  try {
    const { data } = await fetchNoticeList(query.value);

    noticeLoading.value = false;
    noticeList.value = data.rows.map((e): DesktopNotice => {
      return {
        color: 'green',
        title: e.noticeTitle,
        content: e.noticeContent,
        type: 'system',
        id: e.noticeId,
        time: e.updateTime || e.createTime,
        allowClose: false,
      };
    });
  } catch (error) {
    noticeLoading.value = false;
  }
};
export const sortNotice = () => {
  query.value.isAsc = query.value.isAsc === 'asc' ? 'desc' : 'asc';
  getSystemNotice();
};
